using System.Collections;
using System.Collections.Generic;
using Unity.Burst.Intrinsics;
using UnityEngine;

public class Enemy : Entity
{
    [Space]
    [SerializeField] protected LayerMask whanIsPlayer;// 玩家层

    [Header("移动信息 Move info")]
    [Tooltip("移动速度")]
    public float moveSpeed;
    [Tooltip("待机时间")]
    public float idleTime;
    [Tooltip("战斗时间")]
    public float battleTime;
    private float defaultMoveSpeed;//默认速度


    [Header("攻击信息 Attack info")]
    [Tooltip("攻击距离")]
    public float attackDistance;// 攻击距离
    [Tooltip("攻击冷却")]
    public float attackCooldown;// 攻击冷却
    public float minAttackCooldown;
    public float maxAttackCooldown;
    public float lastTimeSttacked;// 最后一次攻击时间
    public float DiscoveredPlayerDistance = 50;// 发现玩家的距离

    [Header("受格挡信息 Stunned info")]
    [Tooltip("受格挡时间")]
    public float stunDuration;
    [Tooltip("受格挡位移")]
    public Vector2 stunDirection;
    protected bool canBeStunned;// 是否可格挡
    [Tooltip("攻击预警方块")]
    [SerializeField] protected GameObject couterImage;

    public EnemyStateMachine stateMachine { get; private set; }
    public string lastAnimBoolName { get; private set; }

    protected override void Awake()
    {
        base.Awake();
        stateMachine = new EnemyStateMachine();// 实例化状态机
        defaultMoveSpeed = moveSpeed;//初始化默认速度
    }

    protected override void Start()
    {
        base.Start();
    }

    protected override void Update()
    {
        base.Update();
        stateMachine.currentState.Update();

    }

    // 寒冷减速
    public override void SlowEntityBy(float _slowPercentage, float _slowDuration)
    {
        moveSpeed = moveSpeed * (1 - _slowPercentage);
        anim.speed = anim.speed * (1 - _slowPercentage);
        Invoke("ReturnDefaultSpeed", _slowDuration);
    }
    // 变为正常状态
    protected override void ReturnDefaultSpeed()
    {
        base.ReturnDefaultSpeed();
        moveSpeed = defaultMoveSpeed;
    }

    // 保存死亡前动画名称
    public virtual void AssignLastAnimName(string _animBoolName)
    {
        lastAnimBoolName = _animBoolName;
    }

    // 冻结敌人
    public virtual void FreezeTime(bool _timeFrozen)
    {
        if (_timeFrozen)
        {
            moveSpeed = 0;
            anim.speed = 0;
        }
        else
        {
            moveSpeed = defaultMoveSpeed;
            anim.speed = 1;
        }
    }

    // 使用协程让敌人冻结
    protected virtual IEnumerator FreezeTimerCorouttine(float _seconds)
    {
        FreezeTime(true);
        yield return new WaitForSeconds(_seconds);
        FreezeTime(false);
    }

    // 让外部调用冻结协程
    public virtual void FreezeTimeFor(float _duration) => StartCoroutine(FreezeTimerCorouttine(_duration));

    //攻击敌人让它向前玩家面向方向移动
    protected override IEnumerator HitKnockback()
    {
        isKnocked = true;
        rb.velocity = new Vector2(knockbackDirection.x * PlayerManager.instance.player.facingDir, knockbackDirection.y);
        yield return new WaitForSeconds(knockbackDuration);
        isKnocked = false;
        SetZeroVelocity();
    }

    //攻击预警
    public virtual void OpenCounterAttackWindow()
    {
        canBeStunned = true;//攻击时可以进行格挡
        couterImage.SetActive(true);
    }
    public virtual void CloseCounterAttackWindow()
    {
        canBeStunned = false;
        couterImage.SetActive(false);
    }

    // 判断是否攻击处于可以被格挡的状态
    public virtual bool IsCanBeStunned()
    {
        if (canBeStunned)
        {
            CloseCounterAttackWindow();
            return true;
        }
        return false;
    }
    // 发现玩家标志
    public virtual RaycastHit2D IsPlayerDetected() => Physics2D.Raycast(wallCheck.position, Vector2.right * facingDir, DiscoveredPlayerDistance, whanIsPlayer);
    // 动画结束事件/标志
    public virtual void AnimationFinishTrigger() => stateMachine.currentState.AnimationFinishTrigger();
    // 画攻击距离线
    protected override void OnDrawGizmos()
    {
        base.OnDrawGizmos();
        Gizmos.color = Color.green;
        Gizmos.DrawLine(transform.position, new Vector3(transform.position.x + attackDistance * facingDir, transform.position.y));
    }
}
